package com.llamarun;

import java.io.File;
import java.io.FileInputStream;
import java.io.ObjectInputStream;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;



public class LlamaRunActivity extends Activity 
{
    /** Called when the activity is first created. */
	private boolean beenstopped = false;
	private static final String TAG = LlamaRunActivity.class.getSimpleName();
	//BackGroundMusic myMusic = new BackGroundMusic(this);
	MediaPlayer mp;// = MediaPlayer.create(LlamaRunActivity.this, R.raw.llamasong);
	//BackgroundSound mBackgroundSound = new BackgroundSound();
	Music myMusic;
	
//	public class BackgroundSound extends AsyncTask<Void, Void, Void> {
//
//	    @Override
//	    protected Void doInBackground(Void... params) {
//	        MediaPlayer player = MediaPlayer.create(LlamaRunActivity.this, R.raw.llamasong); 
//	        player.setLooping(true); // Set looping 
//	        player.setVolume(100,100); 
//	        player.start(); 
//
//	        return null;
//	    }
//
//	}
	
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
    	super.onCreate(savedInstanceState);    
   
    	mp = MediaPlayer.create(LlamaRunActivity.this, R.raw.llamarunmenusong);
    	mp.setLooping(true);
    	
        // making it full screen
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
        //Remove title bar
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        
        setContentView(R.layout.main);
                
    	TextView highScoreValue = (TextView) findViewById(R.id.highScoreValue);
    	int score = readHighScore();
    	highScoreValue.setText(Integer.toString(score));
    	//highScoreTxt.append("LOOOL");
    	//highScoreTxt.te
        
        
//        //myMusic.execute(null);
//    	if(!mp.isLooping()) mp.setLooping(true);
//    	if(!mp.isPlaying()) mp.start();
        
        final Button startButton = (Button) findViewById(R.id.gameBtn);
        startButton.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View view) 
            {
            	//showToastMessage("Hi");
            	Intent myIntent = new Intent(view.getContext(), GameActivity.class);
                startActivityForResult(myIntent, 0);
            }
        });
        
//        final Button optionsButton = (Button) findViewById(R.id.optionsBtn);
//        optionsButton.setOnClickListener(new View.OnClickListener() 
//        {
//            public void onClick(View view)
//            {
//            	//showToastMessage("Hi");
//            	Intent myIntent = new Intent(view.getContext(), OptionsActivity.class);
//                startActivityForResult(myIntent, 0);
//            }
//        });
        
    }

    private int readHighScore()
    {
    	File file=new File(getFilesDir(), "highScore.dat");
        try {

          FileInputStream fint = new FileInputStream(file);
          ObjectInputStream ois = new ObjectInputStream(fint);
          int score=(Integer) ois.readObject();
          ois.close();
          return score;
        }
        catch (Exception e) { 
        	e.printStackTrace(); 
         }
		return 0;
    }
  
	
    
    @Override
    protected void onDestroy()
    {
    	//myMusic.cancel(true);
    	super.onDestroy();
    }
     
    @Override
    protected void onStop() 
    {
    	beenstopped = true;
    	//myMusic.cancel(true);
    	super.onStop();
    }
    
    @Override
    protected void onResume()
    {
     	TextView highScoreValue = (TextView) findViewById(R.id.highScoreValue);
    	int score = readHighScore();
    	highScoreValue.setText(Integer.toString(score));
    	super.onResume();
    	
    	if(!mp.isPlaying()) mp.start(); 
    	//mBackgroundSound.execute(null);
    	//if(beenstopped && myMusic.isCancelled()) myMusic.execute();
    	
    }
    
    @Override
    protected void onPause()
    {
    	super.onPause();
    	//mBackgroundSound.cancel(true);
    	if(mp.isPlaying()) mp.pause();
    }
    
    
    private void showToastMessage(String msg){

        Toast toast = Toast.makeText(this, msg, Toast.LENGTH_SHORT);

        toast.show();

    }
    
    
   
}